1.a) Git introduction

Published

Sat, 20 of April, 2024

Modified

Sat, 20 of April, 2024

Caution

Web page construction in progress…

First: some useful terminal commands

  • [shift+Q] –> to exit from git dialogue
  • highlight + [cmd + D] –> to seleect all subsequent instances
  • highlight + [alt + drag] –> to edit at begin of each lines
  • highlight + [cmd + shift + L] –> to edit at the end of each lines
  • highlight + [ctr + shift + W] –> wrapping text with start and end tag

Git installation

2 recommmended options for Mac:

  1. Installer websites

  2. You should check out Homebrew, “the missing package manager for OS X”. Among many other things, it can install Git for you. Once you have Homebrew installed, do this in the shell:

brew install git

Check version

which git
git --version

Git configuration

In the shell (Appendix A):

git config --global user.name "Jane Doe"
git config --global user.email "jane@example.com"
git config --global --list

In R

The usethis package offers an alternative approach. You can set your Git user name and email from within R:

## install if needed (do this exactly once):
## install.packages("usethis")

library(usethis)
#usethis::use_git_config(user.name = "Jane Doe", user.email = "jane@example.org")

Git commands

See Figure 1

  • git status = View the state of working directory and staging area
  • git add = Stage changes for next commit
  • git commit = Commit the staged snapshot to the local repository
  • git push = Upload local repository content to a remote repository
    • 4 TEAM: it makes your local changes publicly available in a remote repository.
  • git clone = Copies an entire remote repository down to your local machine, setting up a cloned version and checks out the default branch (generally master)
    • this action is done only once.
  • git fetch = Download content from remote repository, but doesn’t force the merge
    • 4 TEAM: if a developer has pushed changes to a remote branch, those changes will be pulled down to your repository whenever fetch is performed.
      • Note: fetch won’t automatically merge any changes, only update references!
  • git merge = Join two branches together
  • git pull = Combo of git fetch and git merge
Figure 1: Git Workflow

Source: cloudstudio.com.au

Git repo setup (1st Github >> 2nd Local)

Recommended!!! (because this method sets up the local Git repository for immediate pulling and pushing. Under the hood, we are doing git clone.)

1) On Github I create a NEW repo

[…]

2.a) In Terminal clone remote repo locally

i) position myself in the Folder ABOVE the desired git repo

#  My `ParentFolder` for github repos...
cd cd ~/Github/  

ii) clone remote repo locally locally

You can only push to two types of URL addresses:

  • An HTTPS URL like https://github.com/user/repo.git
  • An SSH URL, like git@github.com:user/repo.git

Git associates a remote URL with a name, and your default remote is usually called origin

# if https (Clone using the web URL.)
git clone https://github.com/Lulliter/nerd_help.git
# if SSH (Use a password-protected SSH key.)
git clone git@github.com:Lulliter/nerd_help.git

2.b) With {usethis} clone remote repo locally

In the R console (in any R session)

  • OKKIO: only need the parent folder name (~/Github/) because the actual folder name will be the repo name !
library(usethis)
usethis::create_from_github(
    # https or SSH both ok 
    repo_spec = "git@github.com:Lulliter/ideeperpavia.git",
    # destdir specifies the parent directory where the new folder (and local Git repo) will live. 
    destdir = "~/Github/"
)

This Creates a new local directory in destdir, which is all of these things:

  • a directory or folder on your computer
  • a Git repository, linked to a remote GitHub repository
  • an RStudio Project

Opens a new RStudio instance in the new Project

3) - OPTIONAL - I copy there some stuff I already had in a different folder

# copy other LOCAL --> git LOCAL
cp -r ~/OtherLocalDrive/nerd_help/   ~/Github/_nerd_help 

4) I make changes in the local repo

For example, I actually wanted a quarto website, so I open TERMINAL and do

quarto create project website mysite

5) Check what happened…

cd ~/Github/_nerd_help 
git remote show origin 
git status

6) Add local changes to staging area

git add -A # everything
git add -u # only already tracked stuff 

7) Commit desired changes

# Create Std commit "message"....
msg="rebuilt on `date`"
if [ $# -eq 1 ]
  then msg="$1"
fi
# ... Commit Those changes.
git commit -m "$msg"

# or  
git commit -m "whatever message"

8) Push source and build repos.

git push origin master

Git architecture

  • origin = stands for the remote repository. When we use git push -u origin local_branch, it tells the system that we want to push our local branch to the remote repository. Usually there is one default remote repository and origin represents this default repository.
    • If you don’t like “origin”, you can rename it
#  rename origin it by using 
git remote rename origin new_name
  • branch branch is a like a fork in the history of a repository. One branch represents an independent line of development, like a fork teeth.
#  to check which branches I have  
git branch -a
    # * master
    #   page_col
    #   remotes/origin/master
  • master master is a branch, the default branch, the main branch, and it’s always there.

  • HEAD = the currently active branch (the checked out one). Each repository only has one current branch, hence one HEAD as well.

    • Detached HEAD happens when a checkout command is applied to a specific historical commit, tag or remote branch.
#  to check where the HEAD of a repository is pointing to
cat .git/HEAD
    # ref: refs/heads/master
  • index index is the proposed next commit, also called staging area.

See differences b/w commits

# diff between 2 latest commits (1 on branch) only in files I care 
git diff dc87ae c86edffc16 "*.qmd"

#(To view diff between next commit (HEAD) and parent commit (SHA 682bc))
git diff dc87ae..  
git diff dc87ae^..HEAD  '***.qmd' #ORQ
git diff dc87ae..HEAD   #OR

# b/w old commit on branch and HEAD 
git diff 693e61^..HEAD

Problems

Your branch is ahead of ‘origin/master’ by “x” commits

Ex when I change Github Pages because of AWS domain

  1. make a BACKUP of local!

  2. Brutal: just local changes or reset your local master to the state on remote

git reset --hard origin/master

Reference